<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  Gamer Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage Gamer  
 * @author Manh Nguyen - 08/08/2012 
 * @link http://sunsoft.vn
 */
class Items_model extends DataMapper {

    var $model = 'items_model';
    var $table = 'items';
    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct($id = NULL) {
        parent::__construct($id);
        
        $this->validation = array(
                        'stNew'=>array(
                            'values'=>array(
                                '1'=>$this->lang->line('yes'),
                                '0'=>$this->lang->line('no')
                            )
                        ),
                        'stBestBuy'=>array(
                            'values'=>array(
                                '1'=>$this->lang->line('yes'),
                                '0'=>$this->lang->line('no')
                            )
                        )
        );
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    var $validation = array();
    // --------------------------------------------------------------------

    /**
     * Default order in query select 
     */
    var $default_order_by = array('id');

    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? '---empty---' : $this->title;
    }
    
    function check_exist($itemId = '')
    {
        $this->db->select('itemId')
                ->where('itemID', $itemId)
                ->get();
    }
    
    function get_stNew()
    {
        return $this->validation['stNew']['values'][$this->stNew];
    }
    
    function get_stBestBuy()
    {
        return $this->validation['stNew']['values'][$this->stBestBuy];
    }
    
    function option_stNew()
    {
        return $this->validation['stNew']['values'];
    }
    
    function option_stBestBuy()
    {
        return $this->validation['stBestBuy']['values'];
    }
    
    function get_item_new()
    {
        $query = $this->db->select('news.id, news.title, news.code, news.description, news.image, news.old_price, news.new_price, news.category_id, category.code AS category_code')
                ->from('news')
                ->join('items','news.id=items.itemID')
                ->join('category','news.category_id=category.id')
                ->where('items.stNew',1)
                ->order_by('items.dateNewInsert','DESC')
                ->get();
        return $query->result();
    }
    
    function get_item_bestbuy()
    {
        $query = $this->db->select('news.id, news.title, news.code, news.description, news.image, news.old_price, news.new_price, news.category_id, category.code AS category_code')
                ->from('news')
                ->join('items','news.id=items.itemID')
                ->join('category','news.category_id=category.id')
                ->where('items.stBestBuy',1)
                ->order_by('items.dateBestBuyInsert','DESC')
                ->get();
        return $query->result();
    }
    
    function jqgrid_metadata() {

        //edittype: 'text', 'textarea', 'select', 'checkbox', 'password', 'button', 'image', 'file', 'custom'
        return array(
            'names' => array(
                'id',
                'ID sản phẩm',
                'Tiêu Đề',
                'Hàng mới',
                'Ngày thêm mới',
                'Hàng bán chạy',
                'Ngày thêm bán chạy'
            ),
            'model' => array(
                array(
                    'name'=>'id',
                    'hidden'=>TRUE,
                    'key'=>TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'itemID',
                    //'hidden' => FALSE,
                    //'key' => TRUE,
                    //'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'stNew',
                        'search'=>FALSE
                ),
                array(
                    'name'=>'dateNewInsert',
                    'search'=>FALSE
                ),
                array('name' => 'stBestBuy',
                        'search'=>FALSE
                ),
                array(
                    'name'=>'dateBestBuyInsert',
                    'search'=>FALSE
                )
            )
        );
    }
}

/* End of file configuration.php */
/* Location: ./application/models/configuration.php */